#include "cjsonconfigz.h"

cjsonconfigz &cjsonconfigz::Instance() {
    static cjsonconfigz instance;
    return instance;
}

cjsonconfigz::cjsonconfigz()
    : m_configFile("default.json") {}

void cjsonconfigz::InitCfgFile(string filename) {
    m_configFile = filename;
    // 从配置文件中读取配置信息
    std::ifstream ifs(m_configFile.c_str());
    if (ifs.is_open()) {
        Json::Value root;
        Json::CharReaderBuilder builder;
        builder["collectComments"] = true;
        JSONCPP_STRING errs;

        if (!parseFromStream(builder, ifs, &root, &errs)) {
            // 失败了
            ifs.close();
            m_jsondata = m_defaultjson;
            SaveJsonFile();

        } else {
            m_jsondata = root;
            ifs.close();
        }

    } else {
        // 无法打开配置文件，创建一个新的空配置信息
        m_jsondata = m_defaultjson;
        SaveJsonFile();
    }
}

void cjsonconfigz::SaveJsonFile() {
    //
    ofstream ofs(m_configFile.c_str());
    if (ofs.is_open()) {
        Json::StreamWriterBuilder builder;
        const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
        writer->write(m_jsondata, &ofs);
        ofs.flush();
        ofs.close();
    }
}

void cjsonconfigz::SetdefultCFG(string skey, string svalue) {
    // 保存成默认Json
    m_defaultjson[skey] = svalue;
}

void cjsonconfigz::SetdefultCFG(string skey, int ivalue) {
    m_defaultjson[skey] = ivalue;
}

string cjsonconfigz::getStringbyKey(string skey) {
    if (m_jsondata.isMember(skey)) {
        //
        if (m_jsondata[skey].isString()) {
            return m_jsondata[skey].asString();
        }
    }
    if (m_defaultjson.isMember(skey)) {
        if (m_defaultjson[skey].isString()) {
            m_jsondata[skey] = m_defaultjson[skey];
            return m_jsondata[skey].asString();
        }
    }
    return "";
}

int cjsonconfigz::getIntbyKey(string skey) {
    if (m_jsondata.isMember(skey)) {
        //
        if (m_jsondata[skey].isInt()) {
            return m_jsondata[skey].asInt();
        }
    }
    if (m_defaultjson.isMember(skey)) {
        //
        if (m_defaultjson[skey].isInt()) {
            m_jsondata[skey] = m_defaultjson[skey];
            return m_jsondata[skey].asInt();
        }
    }
    return -1;
}

Json::Value cjsonconfigz::getArraybyKey(string skey) {
    if (m_jsondata.isMember(skey)) {
        if (m_jsondata[skey].isArray()) {
            return m_jsondata[skey];
        }
    }
    return Json::Value();
}

void cjsonconfigz::setArrayKey(string skey, Json::Value array) {
    m_jsondata[skey] = array;
    SaveJsonFile();
}

int cjsonconfigz::SaveValuebyKey(string skey, string svalue, bool bstr) {
    int ires = 0;
    if (bstr == false) {
        if (svalue.empty()) {
            svalue = "";
        }
        string stmp = getStringbyKey(skey);
        if (stmp != svalue) {
            m_jsondata[skey] = svalue;
            ires = 1;
            SaveJsonFile();
        }

    } else {
        try {
            int ivalue = stoi(svalue);
            int itmp = getIntbyKey(skey);
            if (itmp != ivalue) {
                m_jsondata[skey] = ivalue;
                ires = 1;
                SaveJsonFile();
            }
        } catch (const std::invalid_argument &e) {
        } catch (const std::out_of_range &e) {
        }
    }
    return ires;
}
